package com.smh.a04XmlDemo2;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

public class Dom4jDemo {
    public static void main(String[] args) throws DocumentException {

        //获得解析器对象
        SAXReader saxReader = new SAXReader();
        //利用解析器去读xml文件,并返回文档对象
        File file = new File("day32-code\\src\\com\\smh\\a03XmlDemo\\book2.xml");
        Document document = saxReader.read(file);
        //打印
        System.out.println(document);

        //获取根标签
        Element rootElement = document.getRootElement();
        System.out.println(rootElement);
        //获取所有子元素
        System.out.println("------------------");
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            System.out.println(element.getName());

//            Attribute id = element.attribute("id");
//            String idValue = id.getText();
//            System.out.println(idValue);
            Element 书名 = element.element("书名");
            System.out.println(书名.getText());
            Element 作者 = element.element("作者");
            System.out.println(作者.getText());
            Element 售价 = element.element("售价");
            System.out.println(售价.getText());


//            List<Element> elements1 = element.elements();
//            for (Element element1 : elements1) {
//                System.out.println(element1.getName());
//            }
        }


    }
}
